<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

require_once dirname(__FILE__).'/menu.php';

if(!class_exists('PC_controller')){
	class PC_controller extends CI_Controller {

		public function __construct(){
			parent::__construct();

			if(!in_array(strtolower($this->router->fetch_method()),array_map('strtolower', get_class_methods($this)))){
				show_404();
			}

			$this->load->model('Appmodel','m_app');

			if(!$this->m_app->checkUserLogin()){
				if($_SERVER['REQUEST_METHOD'] == 'GET'){
					redirect('admin/login');
				}else{
					$this->response->setDetail($this->lang->line('error_login_timeout'));
					$this->response->setAddon(array(
						'code' => RESPONSE_CODE_UNAUTHORIZED,
					));
					$this->response->responseExit();
				}
			}elseif(!$this->m_app->checkUserAccess($this->uri->rsegments[1],$this->uri->rsegments[2])){
				if($_SERVER['REQUEST_METHOD'] == 'GET'){
					redirect('admin/login');
				} else {
					$this->response->setDetail($this->lang->line('error_permission'));
					$this->response->responseExit();
				}
			}
			$this->load->database();
		}

		public function __destruct() {
			$this->db->close();
		}

		public function index(){
			$data = array();
			$data['username'] = $this->m_app->getCurrentUserName();
			$data['permission'] = $this->m_app->outputPermission($this->router->class);
			
			$ctrlName = $this->uri->rsegments[1];
			$data['ctrlName'] = $ctrlName;
			$mnu_submenu = array();
			$baseUrl = base_url().'admin/';
			if(PERMISSION_DISABLE) {
				$menu = $this->m_app->initMenu();
			} else {
				$menu = $this->m_app->getMenu();
			}

			foreach ($menu['base_mnu'][$menu[$ctrlName][0]] as $key => $value) {
				$row = array();
				$row['title'] = $key;
				$row['childs'] = array();
				foreach ($value as $k1 => $v1) {
					$row['childs'][] = array(
						'url' => $baseUrl.$v1,
						'text' => $k1,
					);
				}
				$mnu_submenu[] = $row;
			}

			$data['menu']['mnu_layout'] = json_encode($menu['mnu_layout']);
			$data['menu']['mnu_submenu'] = json_encode($mnu_submenu);
			$data['menu']['nav_active'] = $menu[$ctrlName][0];
			$data['menu']['nav_title'] = $menu[$ctrlName][2];
			$data['menu']['mnu_url'] = $menu[$ctrlName][4];
			// $menu = $this->session->userdata('menu');

			return $data;
		}
	}
}

/* End of file pc_controller.php */
/* Location: ./application/controllers/pc_controller.php */